﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace firstrpg
{
    public enum itemtype
    {
        none = 0,
        laser = 1,

        total = 0,
    }
    public abstract class item : atom
    {
        byte owner = 0;     //the index of the owner
        bool owned = false;
        public object loc;
        public body pulled = null;
        itemtype type = itemtype.none;
        public Vector2 center_position
        {
            get
            {
                return new Vector2(width *0.5f, height * 0.5f);
            }
        }

        public item() { this.SetItem(); }
        public void update(GameTime gameTime)
        { }
        public void draw(GameTime gameTime, SpriteBatch spriteBatch) { }

        /// <summary>
        /// Set up the item's characteristics/stats
        /// </summary>
        public virtual void SetItem()
        { }

        public static void Create_Item_At_Location(int x, int y, itemtype what)
        {
            item newitem = null;
            switch (what)
            {
                case itemtype.laser:
                    newitem = new laser();
                    break;
            }
            if (newitem != null)
            {
                newitem.position = new Vector2((float)x, (float)y);
                client.items.Add(newitem);
                alert_message.message_alert("new item created " + newitem.name + " at " + x + ";" + y);

            }


        }


        /// <summary>
        /// Calls base.memberwiseclone(); returns object
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return base.MemberwiseClone();
        }

       


        /// <summary>
        /// Returns true/false if this item is owned
        /// </summary>
        /// <returns></returns>
        public bool is_owned()
        {
            if (this.owned)
                return true;
            return false;
        }
        public virtual void Update(GameTime gameTime)
        {
            if (this.pulled != null)
                this.velocity = this.pulled.velocity;
            this.position += this.velocity;
        }
        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (spriteBatch == null) return;

            spriteBatch.Draw(client.item_texture[0],
                this.position,
                null,
                Color.White,
                this.rotation,
                this.center_position,
                1.0f,
                SpriteEffects.None,
                0.0f);

        }
    }



    class laser : item
    {
        public override void SetItem()
        {
            base.SetItem();
            this.name = "laser dogg locator";

        }
    }






    /// <summary>
    /// Contains a list of all items in the world, and if they are in inventory/bag/banks etc...
    /// </summary>
    public class itemContainer : List<item>
    {
        
        public void Update(GameTime gameTime)
        {
            if (this.Count > 0)
            {
                foreach (item I in this)
                {
                    I.Update(gameTime);
                }

            }
        }
        /// <summary>
        /// Returns a list[] format of owned items in the item container
        /// </summary>
        /// <returns></returns>
        public item[] get_owned_items()
        {
            item[] retlist = new item[0];
            List<item> list = new List<item>();

            #region check_if_count_exists
            if (this.Count < 0)
            {
                Console.WriteLine("no owned items in get_owned_items, returning nothing");
                return retlist;
            }
            #endregion

            foreach (item I in this)
            {
                if (I.is_owned())
                    list.Add(I);
            }

            retlist = list.ToArray();
            return retlist;
        }

        /// <summary>
        /// Returns a List of all items within a range;
        /// <para>range = 5m (80 pixels)</para>
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        public List<item> get_items_in_range(int range = 5)
        {
            List<item> retlist = new List<item>();
            range = range * 16; //every 16 pixels = 1m; default range is 5m
            Rectangle _range = new Rectangle((int)client.mob.position.X - range, (int)client.mob.position.Y - range, range * 2, range * 2);

            foreach (item finditem in this)
            {
                if (_range.Contains((int)finditem.position.X, (int)finditem.position.Y))
                {
                    retlist.Add(finditem);
                }

            }


            return retlist;
        }

        /// <summary>
        /// Returns item closest to client.mob
        /// </summary>
        /// <returns></returns>
        public static item get_closest_item(int range = 5)
        {
            item grab = null;
            float dist = 0;
            float odist = 0;
            Vector2 centre = client.mob.position;
            List<item> items_in_range = client.items.get_items_in_range(range);
            int counter = 0;

            foreach (item what in items_in_range)
            {
                counter++;
                if (counter == 1)
                {
                    odist = Vector2.Distance(centre, what.position);
                    grab = what;
                    continue;
                }
                dist = Vector2.Distance(centre, what.position);
                if (what.is_owned())
                    continue;
                if (dist < odist)
                    grab = what;
                odist = dist;
            }
            return grab;
        }

        

    }
}
